package com.guigu.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName:RabbitAutoConfiguration
 * @Description://TODO
 * @Author:taoLong
 * @Date:2021/11/2:14:53 星期二
 * @email:2391611102@qq.com or 2391611102qq@gmail.com
 */
@Configuration
@EnableRabbit
public class RabbitAutoConfiguration {

    /**
     * 直连交换机
     */
    private static final String EXCHANGE_NAME = "DirectExchange";
    /**
     * 支付队列
     */
    private static final String PAY_QUEUE = "payQueue";

    /**
     * 路由路径
     */
    private static final String ROUTING_KEY = "pay";


    /**
     * 配置死信交换机
     */

    /**
     * 死信交换机
     */
    private static final String DIED_EXCHANGE_NAME = "diedExchange";
    /**
     * 死信队列
     */
    private static final String DIED_QUEUE_NAME = "diedQueue";
    /**
     * 死信路由key
     */
    private static final String DIED_ROUTE_KEY = "diedKey";


    /**
     * 声明一个死信交换机
     * 死信队列跟交换机类型没有关系 不一定为directExchange  不影响该类型交换机的特性.
     *
     * @return
     */
    @Bean
    public DirectExchange diedExchange() {
        return new DirectExchange(DIED_EXCHANGE_NAME, false, true);
    }


    /**
     * 创建一个死信队列
     *
     * @return
     */
    @Bean
    public Queue diedQueue() {
        return QueueBuilder.durable(DIED_QUEUE_NAME).build();
    }


    /**
     * 将死信队列绑定到死信交换机
     *
     * @param diedQueue
     * @param diedExchange
     * @return
     */
    @Bean
    public Binding diedBindingWithDiedKey(@Qualifier("diedQueue") Queue diedQueue, @Qualifier("diedExchange") DirectExchange diedExchange) {
        return BindingBuilder.bind(diedQueue).to(diedExchange).with(DIED_ROUTE_KEY);
    }

    /**
     * 配置直连交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    /**
     * 配置广告位队列
     *
     * @return
     */
    @Bean(value = "adQueue")
    public Queue adQueue() {
        Map<String, Object> map = new HashMap<>();
        //设置超时时间
        map.put("x-message-ttl", 180000);//超时时间,如果超过了这个时间,则会进行死信队列,或者销毁
        map.put("x-dead-letter-exchange", DIED_EXCHANGE_NAME);//死信交换机名称
        map.put("x-dead-letter-routing-key", DIED_ROUTE_KEY);//死信路由key
        return QueueBuilder.durable(PAY_QUEUE).withArguments(map).build();
    }


    /**
     * 队列和交换机绑定
     *
     * @param adQueue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding adQueueWithUpdate(Queue adQueue, DirectExchange directExchange) {
        Binding binding = BindingBuilder.bind(adQueue).to(directExchange).with(ROUTING_KEY);
        return binding;
    }


    @Bean
    @Scope(value = "prototype")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new SerializerMessageConverter());
        return template;
    }

}
